/**
 * 
 */
package com.healthshare.activity.news;



import cn.domob.android.ads.DomobAdView;

import com.healthshare.R;
import com.healthshare.busi.news.ActicleBusi;
import com.healthshare.common.modal.Favorite;
import com.healthshare.parse.news.ActicleParse;
import com.healthshare.tool.GlobalData;
import com.mobclick.android.MobclickAgent;
import com.palmedia.framework.business.BaseBusi;
import com.palmedia.framework.myinterface.UiCallBack;
import com.palmedia.framework.ui.CustomMessageShow;
import com.palmedia.framework.zmfile.ZMFile;
import com.palmedia.framework.zmfile.ZMFilePath;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author xudeliang
 * 文章列表界面
 */
public class ActicleActivity extends Activity implements UiCallBack
{
	
	private TextView acticletitle;
	private TextView acticlefrom;
	private TextView acticlepublictime;
	//private TextView acticlecontent;
	private WebView acticlecontent;
	
	//private ProgressBar progressBar;
	private byte[] contentBuff;
	private String articleid;
	private ActicleParse tlb;
	/**
	 * 
	 */
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		
		setContentView(R.layout.acticle);
		
		// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
		 
		acticletitle =(TextView) findViewById(R.id.acticletitle);
		acticlefrom =(TextView) findViewById(R.id.acticlefrom);
		acticlepublictime =(TextView) findViewById(R.id.acticlepublictime);
		acticlecontent =(WebView) findViewById(R.id.acticlecontent);
		
		((Button)this.findViewById(R.id.backbutton)).setVisibility(View.VISIBLE);
		((Button)this.findViewById(R.id.backbutton)).setTextColor(Color.rgb(0,139,0));
		DomobAdView domob = (DomobAdView) findViewById(R.id.domobAdXML);
    	String kvalue =  MobclickAgent.getConfigParams(this, "advalidate");
    	if(null!=kvalue && kvalue.equals("true"))
    	{
    		domob.setVisibility(View.VISIBLE);
    	}
    	else
    	{
    		domob.setVisibility(View.GONE);
    	}
		
		Button r = ((Button)this.findViewById(R.id.rightbutton));
		r.setVisibility(View.VISIBLE);
		r.setTextColor(Color.rgb(0,139,0));
		r.setText("收藏");
		
		WebSettings setting = acticlecontent.getSettings();
		setting.setJavaScriptEnabled(true);// 可用JS
		setting.setSupportZoom(true); // 设置可以支持缩放
		setting.setBuiltInZoomControls(true); // 设置出现缩放工具
		//((Button)this.findViewById(R.id.backbutton)).setVisibility(View.VISIBLE);
		//((Button)this.findViewById(R.id.backbutton)).setTextColor(Color.rgb(46,139,87));
		
		
		Bundle  bb=null;
		if(savedInstanceState!=null)
        {
        	bb=savedInstanceState;
        }
        else
        {
        	bb=this.getIntent().getBundleExtra("articlebundle");
        }
		
		
		/*TextView shareButton = (TextView)findViewById(R.id.share);  
		 shareButton.setOnClickListener(new OnClickListener() {  
	            @Override  
	            public void onClick(View v) { 
	            	String kvalue =  MobclickAgent.getConfigParams(ActicleActivity.this, "shareapk");
	            	
	            	String share = "分享文章<"+tlb.title+">  来自android健康分享： "+kvalue;
	            	Util.shareContent(ActicleActivity.this, share, "分享");
	            }  
	        });  
	        
	        
		 TextView titleButton = (TextView)findViewById(R.id.titleButton);  
	        
	        titleButton.setOnClickListener(new OnClickListener() {  
	            @Override  
	            public void onClick(View v) { 
	            	 finish();
	            }  
	        });  
	        */ 
	       // TextView favoriteButton = (TextView)findViewById(R.id.favorite);  
	        r.setOnClickListener(new OnClickListener() {  
	            @Override  
	            public void onClick(View v) {  
	            	if(null!=contentBuff)
	            	{
	            		Favorite f = new Favorite();
		            	f.acticleid=articleid;
		            	f.title=tlb.title;
		            	f.time=tlb.time;
	            		if(GlobalData.favoriterecs.contains(f))
	            		{
	            			CustomMessageShow.getInst().showShortToast(ActicleActivity.this, "已经收藏");
	            			return;
	            		}
	            		
	            		ZMFilePath favorite = new ZMFilePath();
		            	favorite.pushPathNode("favorite");
		            	favorite.addFileName(articleid);
		            	
		            	GlobalData.favoriterecs.add(f);
		            	//boolean ret =ZMFile.write(favorite.toString(), contentBuff, 0, contentBuff.length, 0);
		            	boolean ret1 = GlobalData.getInst().writeFavorite();
		            	if(ret1)
		            	{
		            		CustomMessageShow.getInst().showShortToast(ActicleActivity.this, "收藏成功");
			            	return ;
		            	}
	            	}
	            	CustomMessageShow.getInst().showShortToast(ActicleActivity.this, "收藏失败");
	            	
	            }  
	        });
	        articleid = bb.getString("articleid");
	        ZMFilePath favorite = new ZMFilePath();
        	favorite.pushPathNode("favorite");
        	favorite.addFileName(articleid);
        	contentBuff =ZMFile.readAll(favorite.toString());
        	if(null!=contentBuff)
  	        {
  	        	ActicleBusi ab =new ActicleBusi(null);
  	        	tlb  = (ActicleParse)ab.parseResult(contentBuff);
  	        	showContent(tlb);
  	        }
        	else
        	{
        		 reqActileBusi(articleid);
        	}
	}
	
	public void backButtonAction(View view)
	{
		this.finish();
	}
	
	/**
	 * @author xudeliang
	 * 请求文章列表数据
	 */
	private void reqActileBusi(String acticleid)
	{
		ActicleBusi  ncb = new ActicleBusi(this);
		ncb.acticleid =acticleid;
		ncb.iExecute();
		CustomMessageShow.getInst().showProgressDialog(this);
		//progressBar.setVisibility(View.VISIBLE);
		
	}


	/* (non-Javadoc)
	 * @see com.palmedia.framework.myinterface.UiCallBack#uiCallBack(com.palmedia.framework.business.BaseBusi)
	 */
	@Override
	public void uiCallBack(BaseBusi obj)
	{
		// TODO Auto-generated method stub
		
		if(obj instanceof ActicleBusi)
		{
			//progressBar.setVisibility(View.GONE);
			CustomMessageShow.getInst().cancleProgressDialog();
			ActicleParse tlb = (ActicleParse)obj.getBaseStruct();
			
			if(null ==tlb.content ||tlb.content.length()<=0)
			{
				Toast.makeText(this, "数据加载失败，请重试。", Toast.LENGTH_SHORT).show();
				return;
			}
			this.tlb =tlb;
			contentBuff=tlb.contentBuffer;
			showContent(tlb);
			
			if(null!=contentBuff)
			{
				ActicleBusi ab = (ActicleBusi)obj;
		        ZMFilePath favorite = new ZMFilePath();
	        	favorite.pushPathNode("favorite");
	        	favorite.addFileName(ab.acticleid);
	        	ZMFile.write(favorite.toString(), contentBuff, 0, contentBuff.length, 0);
			}
			
		}
	}
	
	

	
	
	//回调刷新界面
	private void showContent(ActicleParse cdp)
	{
		
		/*<html>

		<head>
		  <title>文档的标题</title>
		</head>

		<body>
		  文档的内容... ...
		</body>

		</html>*/
		acticletitle.setText(cdp.title);
		if(cdp.source==null||cdp.source.equals(""))
		{
			acticlefrom.setVisibility(View.GONE);
		}
		else
		{
			acticlefrom.setText(cdp.source);
		}
		if(cdp.time==null||cdp.time.equals(""))
		{
			acticlepublictime.setVisibility(View.GONE);
		}
		else
		{
			acticlepublictime.setText(cdp.time);
		}
		
		acticlecontent.loadDataWithBaseURL (null, cdp.content, "text/html", "utf-8", null);
		//acticlecontent.loadData(cdp.content, "text/html", "utf-8");//.l.setText(cdp.content);
		
	}
	/*public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		 //2、启动次数少于限制次数，返回
		 if(HealthShareActivity.starttime<=30)
		 {
			 return super.onKeyDown(keyCode, event);
		 }
		 else
		 {
			 return true;
		 }
	 }*/
}
